﻿using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;

namespace HtmlHelper
{
    public class GetHtml
    {
        public string UrlFormat(string url)
        {
            url = url.ToLower();
            if (!url.Contains("http:") || !url.Contains("https:"))
            {
                url = url.TrimStart('/');
                url = url.TrimStart('/');
                url = url.TrimStart('/');
                url = "https://" + url;
            }
            return url;
        }
        /// <summary>
        /// 获取网页源码
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string HttpGet(string url)
        {
            url = UrlFormat(url);
            string responsestr = "";
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
            if (req != null)
            {
                req.Accept = "*/*";
                req.Method = "GET";
                req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1";

                using (HttpWebResponse response = req.GetResponse() as HttpWebResponse)
                {
                    Stream stream;
                    if (response != null && response.ContentEncoding.ToLower().Contains("gzip"))
                    {
                        stream = new GZipStream(response.GetResponseStream(), mode: CompressionMode.Decompress);
                    }
                    else if (response != null && response.ContentEncoding.ToLower().Contains("deflate"))
                    {
                        stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress);
                    }
                    else
                    {
                        stream = response?.GetResponseStream();
                    }
                    using (StreamReader reader = new StreamReader(stream, GetEncoding(response.CharacterSet)))
                    {
                        responsestr = reader.ReadToEnd();
                        stream.Dispose();
                    }
                }
            }
            return responsestr;
        }
        public Encoding GetEncoding(string characterSet)
        {
            switch (characterSet)
            {
                case "gb2312": return Encoding.GetEncoding("gb2312");
                case "utf-8": return Encoding.UTF8;
                default: return Encoding.Default;
            }
        }
    }
}
